<template>
    <div>
        <el-dialog
            title="修改密码"
            :visible.sync="dialogVisible"
            :close-on-click-modal="false"
            @closed="close"
            width="600px">
            <el-form :model="formdata" label-width="120px" ref="addform"  :rules="rules">
                <el-form-item label="原始密码：" prop="password">
                    <el-input type="text" v-model.trim="formdata.password" placeholder="请输入原始密码"></el-input>
                </el-form-item>
                <el-form-item label="新的密码：" prop="pass">
                    <el-input type="text" v-model.trim="formdata.pass" placeholder="请输入新的密码"></el-input>
                </el-form-item>
                <el-form-item label="确认密码：" prop="checkPass">
                    <el-input type="text" v-model.trim="formdata.checkPass" placeholder="请再次输入新密码"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="save">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
// 修改密码

import {editPassWord} from '@/api/index.js'
import {passWord} from '@/common/js/validate.js'
export default {
    data() {
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
                if (this.formdata.checkPass !== '') {
                    this.$refs.addform.validateField('checkPass');
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.formdata.pass) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            dialogVisible:false,
            formdata:{
                password:'',
                pass:'',
                checkPass:''
            },
            rules: {
                pass: [
                    { validator: validatePass, trigger: 'blur' },
                    { required: true, message: '新密码不能为空', trigger: 'blur' },
                    ...passWord
                ],
                checkPass: [
                    { validator: validatePass2, trigger: 'blur' },
                    { required: true, message: '确认密码不能为空', trigger: 'blur' },
                    ...passWord
                ],
                password: [
                    { required: true, message: '原始密码不能为空', trigger: 'blur' }
                ]
            }
        }
    },
    methods:{
        save(){
            console.log(JSON.stringify(this.formdata));
            this.$refs.addform.validate((valid) => {
                if (valid) {
                    editPassWord({bOldPwd:this.formdata.password,bNewPwd:this.formdata.checkPass}).then((res) => {
                            if(res) {
                                if(res.code == 200) {
                                    this.$util.tipsMsg('success','修改成功！');
                                    this.dialogVisible = false;
                                } else {
                                    this.$util.tipsMsg('error',res.message);
                                }
                            }
                        }).catch((err) => {

                        })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            })
            
        },
        close(){
            this.$refs.addform.resetFields();
        }
    }
}
</script>
<style lang="less" scoped>

</style>
